package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.TjAllBookAdapter;
import com.shuorikeji.huohuo.adapter.TjTypeAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.BookDetailInfoBean;
import com.shuorikeji.huohuo.bean.BookFlInfoBean;
import com.shuorikeji.huohuo.bean.BookFlMainBean;
import com.shuorikeji.huohuo.bean.TjBookMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.SearchActivity;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MoreBdActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.rv_book)
    RecyclerView rvBook;

    private List<BookFlInfoBean> typeList = new ArrayList<>();//二级分类
    private TjTypeAdapter tjTypeAdapter;
    private int curPage;
    private int pageSize;
    private List<BookDetailInfoBean> tjBookList = new ArrayList<>();
    private TjAllBookAdapter tjAllBookAdapter;
    private int sFlChannelId;//二级分类id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_more_bd);
    }

    @Override
    protected void initData() {
        pageSize = 20;
        initSmartRefreshLayout();
        initAdapter();
        getTjType();
        initClick();
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(MoreBdActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getTjBook();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getTjBook();
            }
        });
    }

    private void initAdapter() {
        rvType.setLayoutManager(new LinearLayoutManager(MoreBdActivity.this, LinearLayoutManager.VERTICAL, false));
        tjTypeAdapter = new TjTypeAdapter(MoreBdActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookFlInfoBean data = (BookFlInfoBean) object;
                        for (int i = 0; i < typeList.size(); i++) {
                            if (data.getId() == typeList.get(i).getId()) {
                                typeList.get(i).setSec(true);
                            } else {
                                typeList.get(i).setSec(false);
                            }
                        }
                        tjTypeAdapter.setData(typeList);
                        sFlChannelId = data.getId();
                        curPage = 1;
                        getTjBook();
                        break;
                }
            }
        });
        rvType.setAdapter(tjTypeAdapter);

        rvBook.setLayoutManager(new LinearLayoutManager(MoreBdActivity.this, LinearLayoutManager.VERTICAL, false));
        rvBook.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        tjAllBookAdapter = new TjAllBookAdapter(MoreBdActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookDetailInfoBean data = (BookDetailInfoBean) object;
                        Intent intent = new Intent(MoreBdActivity.this, BookInfoActivity.class);
                        intent.putExtra("BOOK_ID", data.getId());
                        startActivity(intent);
                        break;
                }
            }
        });
        rvBook.setAdapter(tjAllBookAdapter);
    }

    private void getTjType() {
        mPresenter.getSecFl(MoreBdActivity.this, 2, -1, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        BookFlMainBean data = (BookFlMainBean) object;
                        typeList.clear();
                        typeList.addAll(data.getData());
                        if (typeList != null && typeList.size() > 0) {
                            typeList.get(0).setSec(true);
                            sFlChannelId = typeList.get(0).getId();
                            curPage = 1;
                            getTjBook();
                        }
                        tjTypeAdapter.setData(typeList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MoreBdActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 推荐书籍
     */
    private void getTjBook() {
        mPresenter.getTjBook(MoreBdActivity.this, curPage, pageSize, sFlChannelId, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlLayout.finishLoadMore();
                srlLayout.finishRefresh();
                switch (flag) {
                    case 1:
                        TjBookMainBean data = (TjBookMainBean) object;
                        if (curPage == 1) {
                            tjBookList.clear();
                        }
                        tjBookList.addAll(data.getData().getData());
                        tjAllBookAdapter.setData(tjBookList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MoreBdActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        ivSearch.setOnClickListener(view -> {
            Intent intent = new Intent(MoreBdActivity.this, SearchActivity.class);
            startActivity(intent);
        });
    }
}